package com.wanmait.recycle.redis.dao.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.wanmait.recycle.redis.dao.RedisCollectQuestionDao;

@Repository
public class RedisCollectQuestionDaoImpl implements RedisCollectQuestionDao {

	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	//key为：CollectArticle:1:userinfoId:set
	
	/*添加收藏*/
	@Override
	public void addCollectQuestion(Integer questionId, Integer userinfoId) {
		redisTemplate.opsForSet().add("CollectQuestion:"+questionId+":userinfoId:set", userinfoId.toString());
		redisTemplate.opsForSet().add("CollectQuestion:"+userinfoId+":questionId", questionId.toString());
	}
	
	/*是否收藏*/
	@Override
	public boolean isCollect(Integer questionId, Integer userinfoId) {
		
		return redisTemplate.opsForSet().isMember("CollectQuestion:"+questionId+":userinfoId:set", userinfoId.toString());
		
	}

	/*取消收藏*/
	@Override
	public void removeCollectQuestion(Integer questionId, Integer userinfoId) {
		redisTemplate.opsForSet().remove("CollectQuestion:"+questionId+":userinfoId:set", userinfoId.toString());
		redisTemplate.opsForSet().remove("CollectQuestion:"+userinfoId+":questionId", questionId.toString());
	}

	//类型为String，问题的收藏数量 CollectQuestion:id:count
	/*设置问题收藏数量*/
	@Override
	public void setCount(Integer questionId, Integer CollectCount) {
		redisTemplate.opsForValue().set("CollectQuestion:"+questionId+":CollectCount",CollectCount.toString());;

	}

	/*收藏数加*/
	@Override
	public void increaseCount(Integer questionId) {
		redisTemplate.opsForValue().increment("CollectQuestion:"+questionId+":CollectCount", 1);

	}

	/*收藏数减*/
	@Override
	public void decreaseCount(Integer questionId) {
		redisTemplate.opsForValue().increment("CollectQuestion:"+questionId+":CollectCount", -1);

	}

	//获得收藏数量
	@Override
	public int getCount(Integer questionId) {
			
		try {
			
			String CollectCount = (String)redisTemplate.opsForValue().get("CollectQuestion:"+questionId+":CollectCount");
			return Integer.parseInt(CollectCount);
		} 
		catch (NumberFormatException e) {
			return 0;
		}
		
	}

	@Override
	public void addInit(Integer questionId) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean hasInit(Integer questionId) {
		// TODO Auto-generated method stub
		return false;
	}

	//userLike:list {“questionId”:1,”userinfoId”:1,delete:1,inputTime:毫秒数}
	//delete为1表示取消收藏，为0表示收藏
	@Override
	public void leftPush(String message) {
		redisTemplate.opsForList().leftPush("CollectQuestion:list", message);

	}

	//取出消息
	@Override
	public String rightPop() {
		
		return (String)redisTemplate.opsForList().rightPop("CollectQuestion:list");
	}

	//最近操作了哪些,类型为set，key为userLike:update:set 存放id
	@Override
	public void addUpdate(Integer questionId) {
		redisTemplate.opsForSet().add("CollectQuestion:update:set", questionId.toString());

	}

	//获得上次定时器同步收藏数量后被操作（收藏 取消收藏）的所有的问题id
	/*迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象，
	它提供了一种访问一个容器(container)对象中的各个元素，而又不必暴露该对象内部细节的方法。
	通过迭代器，开发人员不需要了解容器底层的结构，就可以实现对容器的遍历*/
	@Override
	public Set<Integer> getUpdate() {
		//定义类型为Integer的set集合
		Set<Integer> setInts = new HashSet<>();
		//取出redis中的元素
		Set<Object> setObjs = redisTemplate.opsForSet().members("CollectQuestion:update:set");
		//迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象
		Iterator<Object> it = setObjs.iterator();
		while(it.hasNext())
		{
			setInts.add(Integer.parseInt((String)it.next()));
		}
		return setInts;
	}

	/*同步后清空键"CollectQuestion:update:set"中的值*/
	@Override
	public void removeUpdate() {
		redisTemplate.opsForSet().remove("CollectQuestion:update:set");
		
	}
	
	/*获取某个用户收藏的问题时，从redis取出该用户所有收藏的questionId集合*/
	@Override
	public Set<Integer> getCollectQuestionIds(Integer userinfoId) {
			Set<Integer> questionIds = new HashSet<>();
			Set<Object> setObj = redisTemplate.opsForSet().members("CollectQuestion:"+userinfoId+":questionId");
			Iterator<Object> it = setObj.iterator();
			while(it.hasNext()) {
				String questionId = (String)it.next();
				questionIds.add(Integer.parseInt(questionId));
			}
			
			return questionIds;
	}

	
	
	
}
